package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.AAABean;
import com.itboye.eweilisten.model.QuestionBean;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.MyGridView;

import java.util.ArrayList;
import java.util.List;

import static com.itboye.eweilisten.me.personal.activity.PersonalDataActivity.grdaAdapter;

/**
 * Created by itboye on 2017/3/9.
 */

public class GirlGridAdapter extends BaseAdapter {
    Context context;
    private QuestionBean list;
    String one, two;
    private int positionNum;
    private List<AAABean> LiastAbean;
    public static int girlPicture = -1;
    public static int boyPicture = -1;


    public GirlGridAdapter(Context context, QuestionBean list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.getAvatar().size();
    }

    @Override
    public Object getItem(int position) {
        return LiastAbean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHouder houder;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_head_listview_gridview, null);
            houder.name = (TextView) convertView.findViewById(R.id.tvHeadName);
            houder.gridView = (MyGridView) convertView.findViewById(R.id.my_gridview);
            houder.close_iconView = (ImageView) convertView.findViewById(R.id.close_iconView);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }

        List<String> arrayImage = list.getAvatar().get(position).getAvatar();
        for (int i = 0; i < arrayImage.size(); i++) {
            LiastAbean = new ArrayList<>();
            for (int i1 = 0; i1 < arrayImage.size(); i1++) {
                AAABean bean = new AAABean();
                bean.setStr(arrayImage.get(i1));
                LiastAbean.add(bean);
            }
            final GridViewAdapter adaptergirl = new GridViewAdapter(context, arrayImage, girlPicture);
            final GridViewAdapter adapterboy = new GridViewAdapter(context, arrayImage, boyPicture);
            final Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    if (msg.what == 100) {
                        grdaAdapter.notifyDataSetChanged();
                    }

                }
            };
            if (position == 0) {//女生
                houder.gridView.setAdapter(adaptergirl);
                houder.gridView.smoothScrollToPosition(position);
                houder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int positions, long id) {
                        Message msg = new Message();
                        msg.what = 100;
                        girlPicture = positions;
                        boyPicture = -1;
                        handler.sendMessage(msg);
                        String sourceStr = list.getAvatar().get(position).getAvatar().get(positions);
                        System.out.println("sourceStrArray" + positions);
                        positionNum = positions;
                        OnGridview onGridview1 = (OnGridview) context;
                        onGridview1.onBtn(sourceStr);
                        SPUtils.put(context, null, Const.ONE, positionNum + "");
                    }
                });
            } else {//男生
                houder.gridView.setAdapter(adapterboy);
                houder.gridView.smoothScrollToPosition(position);
                houder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int positions, long id) {
                        Message msg = new Message();
                        msg.what = 100;
                        boyPicture = positions;
                        girlPicture = -1;
                        handler.sendMessage(msg);
                        String sourceStr = list.getAvatar().get(position).getAvatar().get(positions);
                        System.out.println("sourceStrArray" + positions);
                        positionNum = positions;
                        OnGridview onGridview1 = (OnGridview) context;
                        onGridview1.onBtn(sourceStr);
                        SPUtils.put(context, null, Const.ONE, positionNum + "");
                    }
                });
            }

        }


        Log.d("IMAGE_HEAD", list.getAvatar().get(position).getAvatar().get(0));
        houder.name.setText(list.getAvatar().get(position).getName());
        if (list.getAvatar().get(position).getCode().equals("00E001003")) {
            houder.close_iconView.setImageResource(R.drawable.girl);
        } else if (list.getAvatar().get(position).getCode().equals("00E001004")) {
            houder.close_iconView.setImageResource(R.drawable.boy);
        }
        return convertView;
    }


    public interface OnGridview {
        void onBtn(String string);
    }

    public class ViewHouder {
        TextView name;

        MyGridView gridView;
        ImageView close_iconView;
    }
}

